
package com.android.fitnesscenter.utils;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;

import android.os.Parcel;
import android.os.Parcelable;

public class MapEntity implements Parcelable, Serializable {

    private static final long serialVersionUID = 1L;

    private HashMap<Integer, Object> map;

    public MapEntity() {
        this.map = new HashMap<Integer, Object>();
    }

    public void setValue(Integer key, Object value) {
        if (value == null)
            return;
        this.map.put(key, value);
    }

    /**
     * Returns the value of the mapping with the specified key.
     * 
     * @param key
     * @return the value of the mapping with the specified key, or null if no
     *         mapping for the specified key is found.
     */
    public Object getValue(Integer key) {
        return map.get(key);
    }

    /**
     * Judge MapEntity for null
     * 
     * @return
     */
    public boolean isEmpty() {
        return map.isEmpty();
    }

    /**
     * clear MapEntity
     */
    public void clear() {
        map.clear();
    }

    public boolean containsKey(Integer key) {
        return this.map.containsKey(key);
    }

    /**
     * Get MapEntity in corresponding key string of value
     * 
     * @param key
     * @return If key exists, return the key corresponding String value; Key
     *         does not exist, to return to ", "and, on logcat print in the log
     */
    public String getString(Integer key) {
        if (!map.containsKey(key)) {
            Loger.e("MapEntity: No such key." + key);
            return "";
        } else {
            return map.get(key).toString();
        }
    }

    /**
     * Get MapEntity in corresponding key of int values
     * 
     * @param key
     * @return If key exists, return the key corresponding int values; Key does
     *         not exist or appear type conversion is unusual, return zero, and
     *         in logcat print in the log
     */
    public int getInt(Integer key) {
        if (!this.map.containsKey(key)) {
            Loger.e("MapEntity: No such key.");
        }
        try {
            return Integer.valueOf(this.map.get(key).toString());
        } catch (NumberFormatException e) {
            Loger.e("NumberFormatException key = " + key + " value = "
                    + this.map.get(key).toString());
        }
        return 0;
    }

    /**
     * Get MapEntity corresponding key in the double values
     * 
     * @param key
     * @return If key exists, return the key corresponding double value; Key
     *         does not exist or appear type conversion is unusual, return to
     *         0.0, and in logcat print in the log
     */
    public double getDouble(Integer key) {
        if (!this.map.containsKey(key)) {
            Loger.e("MapEntity: No such key.");
        }
        try {
            return Double.valueOf(this.map.get(key).toString());
        } catch (NumberFormatException e) {
            Loger.e("NumberFormatException key = " + key + " value = "
                    + this.map.get(key).toString());
        }
        return 0.0;
    }

    /**
     * Get MapEntity in corresponding key value of the float
     * 
     * @param key
     * @return If key exists, return the key corresponding float value; Key does
     *         not exist or appear type conversion is unusual, return to 0.0 f,
     *         and in logcat print in the log
     */
    public float getFloat(Integer key) {
        if (!this.map.containsKey(key)) {
            Loger.e("MapEntity: No such key.");
        }
        try {
            return Float.valueOf(this.map.get(key).toString());
        } catch (NumberFormatException e) {
            Loger.e("NumberFormatException key = " + key + " value = "
                    + this.map.get(key).toString());
        }
        return 0.0f;
    }

    /**
     * Get MapEntity Boolean value of the corresponding key
     * 
     * @param key
     * @return If key exists, return the key corresponding Boolean value; Key
     *         does not exist or appear type conversion is unusual, returns
     *         false, and in logcat print in the log
     */
    public boolean getBool(Integer key) {
        if (!this.map.containsKey(key)) {
            Loger.e("MapEntity: No such key.");
        }
        try {
            return (Boolean) this.map.get(key);
        } catch (ClassCastException ce) {
            Loger.e("MapEntity: ClassCastException.");
        }
        return false;
    }

    /**
     * Get MapEntity corresponding key in the Date value
     * 
     * @param key
     * @return If key exists, return the key corresponding Date value; Key does
     *         not exist or appear type conversion is unusual, returns null, and
     *         in logcat print in the log
     */
    public Date getDate(Integer key) {
        if (!this.map.containsKey(key)) {
            Loger.e("MapEntity: No such key.");
        }
        try {
            return (Date) this.map.get(key);
        } catch (ClassCastException ce) {
            Loger.e("MapEntity: ClassCastException.");
        }
        return null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeMap(map);
    }

    public static final Parcelable.Creator<MapEntity> CREATOR = new Creator<MapEntity>() {

        @SuppressWarnings("unchecked")
        @Override
        public MapEntity createFromParcel(Parcel source) {
            MapEntity mapEntity = new MapEntity();
            mapEntity.map = source.readHashMap(HashMap.class.getClassLoader());
            return mapEntity;
        }

        @Override
        public MapEntity[] newArray(int size) {
            return new MapEntity[size];
        }
    };
}
